<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 *
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 *
 * @package      FeatherCMS
 * @subpackage   Tags
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://julien.portalier.com
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

abstract class TagsController extends Controller
{
	public $moduleClass  = null;
	public $refererClass = null;
	
	public $_templates = 'tags';
	
	function cloud($module_id=null)
	{
		$model = $this->{$this->modelClass};
		
		// about
		if (!is_null($module_id))
		{
			$referer = $model->{is_null($this->refererClass) ? $this->moduleClass : $this->refererClass};
			$referer->conditions(array(
				"{$this->moduleClass}.{$model->{$this->moduleClass}->primaryKey}" => $module_id
			));
			$referer->recursive = 0;
			$this->set('referer', $referer->find());
		}
		
		// cloud
		$this->data = $model->cloud($module_id);
		$this->load->helpers('cloud');
	}
	
	function live($module_id=null)
	{
		$model = $this->{$this->modelClass};
		
		// search
		$search = $this->params['url'][$model->tagName];
		$model->conditions(array($model->tagName => "LIKE $search%"));
		
		// restrict search?
		if ($module_id)
		{
			$model->conditions(array(
				$this->refererClass.'.'.$model->{$this->refererClass}->{$this->moduleClass}->foreignKey => $module_id)
			);
		}
		
		$this->data = $model->listField($model->tagName);
	}
	
	// admin
	
	function manage($module_id)
	{
		$this->__checkAccess("{$this->moduleClass}.$module_id", 'write');
		$this->__referer($module_id);
		$this->__manage($module_id);
	}
	
	function __manage($module_id=null)
	{
		$model = $this->{$this->modelClass};
		$model->id = null;
		
		if (!empty($module_id))
			$model->conditions(array($model->{$this->moduleClass}->foreignKey => $module_id));
		
		if (isset($this->params['url']['q']))
			$model->conditions(array($model->tagName => "LIKE {$this->params['url']['q']}%"));
		
		$model->unbind(array('belongsTo' => $this->refererClass));
		$model->order($model->tagName);
		$this->data = $model->listField($model->tagName, -1);
	}
	
	function rename($module_id, $tag)
	{
		$this->__checkAccess("{$this->moduleClass}.$module_id", 'write');
		$this->__referer($module_id);
		$this->__rename($module_id, $tag);
	}
	
	function __rename($module_id, $tag)
	{
		$model = $this->{$this->modelClass};
		$model->id = null;
		
		if (empty($this->data))
		{
			if (!empty($module_id)) {
				$this->data[$model->name]['module_id'] = $module_id;
			}
			$this->data[$model->name]['old'] = $tag;
			$this->data[$model->name]['new'] = $tag;
		}
		else
		{
			if ($model->rename()) {
				$this->flash(_('The tag has been renamed.'), '/'.$this->viewPath."/manage/$module_id");
			}
			$this->session->setFlash(_('Oops, an error occured.'));
		}
	}
	
	function erase($module_id, $tag)
	{
		$this->__checkAccess("{$this->moduleClass}.$module_id", 'delete');
		$this->__erase($module_id, $tag);
	}
	
	function __erase($module_id, $tag)
	{
		$model  = $this->{$this->modelClass};
		$model->id = null;
		
		if (!empty($module_id))
			$model->conditions(array($model->{$this->moduleClass}->foreignKey => $module_id));
		
		$model->conditions(array($model->tagName => $tag));
		
		if ($model->delete())
			$this->session->setFlash(_('The tag has been erased.'));
		else
			$this->session->setFlash(_('Oops, an error occured.'));
		
		$this->redirect(isset($this->params['url']['referer']) ?
			$this->params['url']['referer'] : getReferer());
	}
	
	// internals
	
	function __referer($module_id)
	{
		$model = $this->{$this->modelClass}->{$this->moduleClass};
		$model->recursive = -1;
		$referer = $model->read('id, name, urlname', $module_id);
		
		if (empty($referer))
			Error::show(404);
		
		$this->set('referer', &$referer);
	}
	
	protected function __checkAccess($aco=null, $action='read')
	{
		$this->acl->checkAccess($aco, $action);
	}
}
?>